package com.yugao.fintech.antelope.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.antelope.base.log.OperateLog;
import com.yugao.fintech.antelope.base.log.enums.BusinessType;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.antelope.system.service.RamResourceService;
import com.yugao.fintech.framework.core.response.Resp;
import com.yugao.fintech.framework.validation.group.UpdateGroup;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 访问资源
 */
@Api(tags = "访问资源")
@RestController
@RequestMapping("/resource")
public class RamResourceController {
    @Autowired
    private RamResourceService ramResourceService;
  
    /**
     * 分页 访问资源
     */
    @GetMapping("/pageResource")
    @RamResource(action = "ram:resource:query", name = "查询访问资源列表", type = ActionType.READ)
    public Resp<IPage<RamResourceResp>> pageResource(@Validated RamResourcePageReq req) {
        return Resp.ok(ramResourceService.pageResource(req));
    }

    @GetMapping("/listResource")
    public Resp<List<RamResourceResp>> listResource(RamResourceListReq query) {
        return Resp.ok(ramResourceService.listResource(query));
    }
  
    /**
     * 通过id查找 访问资源
     */
    @GetMapping("/getResourceById")
    @RamResource(action = "ram:resource:query", name = "查询访问资源", type = ActionType.READ)
    public Resp<RamResourceResp> getResourceById(@RequestParam(value = "id") Long id) {
        return Resp.ok(ramResourceService.getResourceById(id));
    }
  
    /**
     * 保存 访问资源
     */
    @PostMapping("/saveResource")
    @RamResource(action = "ram:resource:save", name = "保存访问资源", type = ActionType.WRITE)
    public Resp<?> saveResource(@Validated @RequestBody RamResourceSaveReq req) {
        return Resp.ok(ramResourceService.saveResource(req));
    }
  
    /**
     * 更新 访问资源
     */
    @PostMapping("/updateResourceById")
    @RamResource(action = "ram:resource:update", name = "更新访问资源", type = ActionType.WRITE)
    public Resp<?> updateResourceById(@Validated(UpdateGroup.class) @RequestBody RamResourceUpdateReq req) {
        ramResourceService.updateResourceById(req);
        return Resp.ok();
    }
  
    /**
     * 批量删除 访问资源
     */
    @DeleteMapping("/deleteResourceByIds")
    @RamResource(action = "ram:resource:delete", name = "删除访问资源", type = ActionType.WRITE)
    public Resp<?> deleteBatch(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(ramResourceService.deleteResourceByIds(ids));
    }
  
    /**
     * 删除 访问资源
     */
    @RamResource(action = "ram:resource:delete", name = "删除访问资源", type = ActionType.WRITE)
    @DeleteMapping("/deleteResourceById")
    public Resp<?> delete(@RequestParam(value = "id") Long id) {
        return Resp.ok(ramResourceService.deleteResourceById(id));
    }

    /**
     * 同步资源 - 这里目前由后管系统应用统一管理，其他租户的应用没有权限同步资源(注意权限分配)
     */
    @OperateLog(summary = "资源管理", businessType = BusinessType.SYNC)
    @RamResource(action = "ram:resource:syncResource", name = "同步资源-专属后管接口", type = ActionType.WRITE)
    @PostMapping("/syncResource")
    public Resp<?> syncResource(@RequestBody @Validated SyncResourceReq req) {
        ramResourceService.syncResource(req);
        return Resp.ok();
    }
}
